home *** CD-ROM | disk | FTP | other *** search
- unit Clmain;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Menus, ExtCtrls, about, inifiles, clsub,
- Buttons;
-
- type
- TForm1 = class(TForm)
- MainMenu1: TMainMenu;
- Game1: TMenuItem;
- New1: TMenuItem;
- Restart1: TMenuItem;
- N1: TMenuItem;
- Bigpanel1: TMenuItem;
- Exit1: TMenuItem;
- Help1: TMenuItem;
- Index1: TMenuItem;
- About1: TMenuItem;
- Image1: TImage;
- Image2: TImage;
- Panel1: TPanel;
- lstep1: TLabel;
- lcycle: TLabel;
- lstep2: TLabel;
- lscore: TLabel;
- Property1: TMenuItem;
- Options1: TMenuItem;
- ldbl: TLabel;
- Backbtn: TSpeedButton;
- procedure About1Click(Sender: TObject);
- procedure Exit1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormPaint(Sender: TObject);
- procedure New1Click(Sender: TObject);
- procedure Restart1Click(Sender: TObject);
- procedure Bigpanel1Click(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- procedure FormKeyPress(Sender: TObject; var Key: Char);
- procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; MX, MY: Integer);
- procedure FormMouseMove(Sender: TObject; Shift: TShiftState; MX,
- MY: Integer);
- procedure Index1Click(Sender: TObject);
- procedure Property1Click(Sender: TObject);
- procedure BackbtnClick(Sender: TObject);
- private
- { Private ÉΘî╛ }
- procedure newgame;
- procedure writeini;
- procedure turn;
- procedure putpanel;
- procedure stg0to1;
- procedure move(xx, yy : integer);
- public
- { Public ÉΘî╛ }
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
- const MAX = 20;
-
- var path : string;
- qstp, loop, size, level, step, nowstp, base, sc : integer;
- org, num : array[1..MAX,1..MAX] of byte;
- before, x, y, stage, psize : byte;
- rect1,rect2 : Trect;
- ans : array[1..2410,0..1] of byte;
-
- procedure Tform1.writeini;
- var clini : TIniFile;
- begin
- clini := TIniFile.Create(path + 'cybrloop.ini');
- try
- with clini do begin
- writeinteger('OPTION','CYCLE',loop);
- writeinteger('OPTION','SIZE',size);
- writeinteger('OPTION','LOOP',step);
- writebool('OPTION','BIGPANEL',bigpanel1.checked);
- writeinteger('WINDOW','LEFT',form1.left);
- writeinteger('WINDOW','TOP',form1.top);
- end;
- finally
- clini.free;
- end;
- end;
-
- procedure rndset(var dx, dy : integer);
- begin
- case random(4) of
- 0 : begin dx := -1; dy := 0; end;
- 1 : begin dx := 1; dy := 0; end;
- 2 : begin dx := 0; dy := -1; end;
- 3 : begin dx := 0; dy := 1; end;
- end;
- end;
-
- procedure Tform1.newgame;
- var sx, sy, i, j, dx, dy, dxx, dyy : integer;
- f : boolean;
- dat : system.text;
- begin
- repeat
- for i := 1 to size do for j := 1 to size do num[j,i] := 0;
- sx := random(size)+1;
- sy := random(size)+1;
- x := sx;
- y := sy;
- rndset(dxx,dyy);
- dx := dxx;
- dy := dyy;
- qstp := 0;
- i := 0;
- f := false;
- repeat
- case random(12) of
- 0..1 : begin j := dx; dx := -dy; dy := j end;
- 2..3 : begin j := dx; dx := dy; dy := -j end;
- end;
- while (x+dx < 1) or (x+dx > size) or (y+dy < 1) or (y+dy > size)
- or ((-dxx=dx) and (-dyy=dy)) do rndset(dx,dy);
- num[x,y] := (num[x,y] + 1) mod loop;
- if num[x,y] = 0 then inc(i);
- x := x + dx;
- y := y + dy;
- dxx := dx;
- dyy := dy;
- inc(qstp);
- ans[qstp,0] := x;
- ans[qstp,1] := y;
- f := (qstp > step * 1.2);
- until f or ((qstp >= step) and (x = sx) and (y = sy));
- until not f;
- ldbl.caption := 'Dbl ' + inttostr(i);
- assignfile(dat,path + 'cybrloop.ans');
- rewrite(dat);
- writeln(dat,'cycle : ',inttostr(loop-1),' steps : ',inttostr(qstp),
- ' dbl : ',inttostr(i));
- for i := 1 to size do begin
- for j := 1 to size do write(dat,num[j,i]);
- writeln(dat);
- end;
- for i := 1 to qstp do begin
- write(dat,'(',ans[i,0]:2,',',ans[i,1]:2,')');
- if i mod 10 = 0 then writeln(dat);
- end;
- closefile(dat);
- org := num;
- clientwidth := psize * size + 72;
- clientheight := psize * size;
- lcycle.caption := 'Cycle : ' + inttostr(loop-1);
- lstep2.caption := inttostr(qstp) + 'step';
- restart1click(New1);
- end;
-
- procedure TForm1.New1Click(Sender: TObject);
- begin
- newgame;
- formpaint(sender);
- end;
-
- procedure TForm1.Restart1Click(Sender: TObject);
- var i, j : byte;
- begin
- num := org;
- x := 1;
- y := 1;
- stage := 0;
- nowstp := 0;
- lstep1.caption := '0/';
- lscore.caption := '0 pts';
- base := 0;
- for i := 1 to size do for j := 1 to size do inc(base,num[j,i]);
- sc := base;
- before := 0;
- backbtn.enabled := false;
- canvas.pen.color := clBlack;
- canvas.brush.color := clBlack;
- formpaint(sender);
- end;
-
- procedure TForm1.Property1Click(Sender: TObject);
- begin
- with form2 do begin
- sploop.value := loop-1;
- spsize.value := size;
- spstep.value := step;
- showmodal;
- if modalresult = mrOk then begin
- loop := sploop.value+1;
- size := spsize.value;
- step := spstep.value;
- new1click(sender);
- end;
- end;
- end;
-
- procedure TForm1.Bigpanel1Click(Sender: TObject);
- begin
- bigpanel1.checked := not bigpanel1.checked;
- if bigpanel1.checked then psize := 32 else psize := 22;
- clientwidth := psize * size + 72;
- clientheight := psize * size;
- formpaint(sender);
- end;
-
- procedure TForm1.Exit1Click(Sender: TObject);
- begin
- writeini;
- application.terminate;
- end;
-
- procedure TForm1.Index1Click(Sender: TObject);
- begin
- application.helpjump('HID_N0001');
- end;
-
- procedure TForm1.About1Click(Sender: TObject);
- begin
- aboutbox.comment.caption := 'Turn all the panels into stars.';
- aboutbox.showmodal;
- end;
-
- procedure TForm1.BackbtnClick(Sender: TObject);
- begin
- dec(sc,num[x,y]);
- num[x,y] := (num[x,y] + 1) mod loop;
- inc(sc,num[x,y]);
- putpanel;
- x := x - ((before mod 16) - 1);
- y := y - ((before div 16) - 1);
- dec(nowstp);
- lstep1.caption := inttostr(nowstp) + '/';
- backbtn.enabled := false;
- if nowstp = 0 then begin
- stage := 0;
- canvas.pen.color := clBlack;
- canvas.brush.color := clBlack;
- end;
- canvas.rectangle((x-1)*psize,(y-1)*psize,x*psize,y*psize);
- if nowstp <= qstp
- then lscore.caption := inttostr(round((1-sc/base)*100)) + ' pts'
- else lscore.caption := 'No pts';
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- var clini : TIniFile;
- begin
- randomize;
- path := extractfilepath(Application.ExeName);
- clini := TIniFile.Create(path + 'cybrloop.ini');
- try
- loop := clini.readinteger('OPTION','CYCLE',3);
- size := clini.readinteger('OPTION','SIZE',10);
- step := clini.readinteger('OPTION','LOOP',40);
- bigpanel1.checked := clini.readbool('OPTION','BIGPANEL',false);
- left := clini.readinteger('WINDOW','LEFT',100);
- top := clini.readinteger('WINDOW','TOP',100);
- finally
- clini.free;
- end;
- if bigpanel1.checked then psize := 32 else psize := 22;
- canvas.brush.style := bsDiagCross;
- newgame;
- end;
-
- procedure Tform1.putpanel;
- begin
- rect1 := rect((x-1)*psize,(y-1)*psize,x*psize,y*psize);
- rect2 := rect(num[x,y]*psize,0,(num[x,y]+1)*psize,psize);
- if bigpanel1.checked
- then canvas.copyrect(rect1,image1.picture.bitmap.canvas,rect2)
- else canvas.copyrect(rect1,image2.picture.bitmap.canvas,rect2);
- end;
-
- procedure TForm1.FormPaint(Sender: TObject);
- var i, j : byte;
- begin
- for i := 1 to size do for j := 1 to size do begin
- rect1 := rect((j-1)*psize,(i-1)*psize,j*psize,i*psize);
- rect2 := rect(num[j,i]*psize,0,(num[j,i]+1)*psize,psize);
- if bigpanel1.checked
- then canvas.copyrect(rect1,image1.picture.bitmap.canvas,rect2)
- else canvas.copyrect(rect1,image2.picture.bitmap.canvas,rect2);
- end;
- canvas.rectangle((x-1)*psize,(y-1)*psize,x*psize,y*psize);
- end;
-
- procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
- begin
- writeini;
- end;
-
- procedure Tform1.turn;
- begin
- dec(sc,num[x,y]);
- num[x,y] := (num[x,y] + loop - 1) mod loop;
- inc(sc,num[x,y]);
- putpanel;
- canvas.rectangle((x-1)*psize,(y-1)*psize,x*psize,y*psize);
- inc(nowstp);
- lstep1.caption := inttostr(nowstp) + '/';
- backbtn.enabled := true;
- if nowstp <= qstp
- then lscore.caption := inttostr(round((1-sc/base)*100)) + ' pts'
- else lscore.caption := 'No pts';
- if sc = 0 then begin
- if nowstp <= qstp
- then aboutbox.comment.caption := 'Congratulations !'
- else aboutbox.comment.caption := 'Too many steps.Retry please.';
- aboutbox.showmodal;
- if nowstp <= qstp then new1click(new1) else restart1click(new1);
- end;
- end;
-
- procedure Tform1.move(xx, yy : integer);
- begin
- if (x+xx >= 1) and (x+xx <= size) and
- (y+yy >= 1) and (y+yy <= size) then begin
- putpanel;
- x := x + xx;
- y := y + yy;
- before := (xx+1) + (yy+1)*16;
- if stage = 1 then turn
- else canvas.rectangle((x-1)*psize,(y-1)*psize,x*psize,y*psize);
- end;
- end;
-
- procedure Tform1.stg0to1;
- begin
- stage := 1;
- canvas.pen.color := clRed;
- canvas.brush.color := clRed;
- turn;
- end;
-
- procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
- begin
- case upcase(key) of
- '-' : if backbtn.enabled then backbtnclick(sender);
- '2' : move(0,1);
- '4' : move(-1,0);
- '5' : if stage = 0 then stg0to1;
- '6' : move(1,0);
- '8' : move(0,-1);
- 'B' : bigpanel1click(sender);
- 'N' : new1click(sender);
- 'P' : property1click(sender);
- 'R' : restart1click(sender);
- end;
- end;
-
- procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; MX, MY: Integer);
- var lx, ly, xx, yy : integer;
- begin
- lx := mx div psize + 1;
- ly := my div psize + 1;
- if (shift = [ssleft]) and (lx <= size) then case stage of
- 0 : stg0to1;
- 1 : begin
- xx := x - lx;
- yy := y - ly;
- if (xx * yy = 0) and (abs(xx+yy) = 1) then move(-xx,-yy);
- end;
- end;
- end;
-
- procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; MX,
- MY: Integer);
- var lx, ly, xx, yy : integer;
- begin
- if stage = 0 then begin
- lx := mx div psize + 1;
- ly := my div psize + 1;
- if (lx <= size) and ((x <> lx) or (y <> ly)) then move(lx-x,ly-y);
- end else if shift = [ssleft] then FormMouseDown(Sender,mbLeft,Shift,MX,MY);
- end;
-
- end.
-